<?php
namespace app1\index\controller;

use think\Controller;
use think\Db;
use app1\index\model\Post;

class Index extends Controller
{

    public $modelpost;

    public $modeluser;

    public $b;

    function index()
    {
        return $this->postlist();
    }

    /**
     * 初始化函数
     */
    public function _initialize()
    {
        $this->user = session('user');
        $this->modelpost = model('Post');
        $this->modeluser = model('User');
    }

    /**
     * 登录函数
     */
    public function login()
    {
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $res = $this->modeluser->login($user_name, $user_password);
        if ($res) {
            session("user", $res);
            $this->user = session("user");
            echo "退出";
        } else {
            $response = "登录失败";
            echo $response;
        }
    }
     
    /**
     * 注册函数
     */
    public function regist()
    {
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $pattern="/[\/,\*,<>,\|,\?]/";
        if(!preg_match($pattern, $user_name)){
            if(!preg_match($pattern, $user_password)){
                if(!($this->modeluser->find($user_name))){
                    $res = $this->modeluser->regist($user_name, $user_password);
                    echo 1;
                }else{
                    echo "文件名重复，请换个文件名";   
                }   
            }else{
                echo "密码不合法，请重新输入密码";
            }
        }else{
            echo "文件名不合法，请重新输入文件名";
        }
    }
    
    // 进入发帖模板
    public function postPage()
    {
        if (NULL == session('user')) {
            $this->success("用户不存在,请先登录再回帖", url('postlist'));
        }
        
        $user = $this->user = session('user');
        return view("postPage", [
            "user" => $user
        ]);
    }
     
    /**
     * 发帖函数
     */
    public function post()
    {
        $title = $_POST['title'];
        $content = $_POST['content'];
        $user_id = $_POST['user_id'];
        $create_time=$_POST['create_time'];
        $data = [
            'title' => $title,
            'content' => $content,
            'create_time' => $create_time,
            'user_id' => $user_id
        ];
        //dump($data);exit;
        $res = $this->modelpost->post($data);
        if ($res) {
            echo 1;
        } else {
            echo 0;
        }
    }
    
    /**
     * 帖子详情函数
     */
    public function postdetail()
    {
        $id = $_REQUEST['id'];
        $list = $this->modelpost->getPageList($id);
        $this->assign('list', $list['list']);
        $this->assign('id', $id);
        $this->assign('page', $list['page']);
        return view('postdetail');
    }

    /**
     * 删除帖子函数
     */
    public function postdelete()
    {
        $id = $_POST['id'];
        $pid = $_POST['pid'];
        $res = $this->modelpost->postdelete($id, $pid);
        if ($res==2) {
            echo 2;
        }elseif($res==1){
            echo 1;
        }else{
            echo 0;
        }
    }

    /**
     * 帖子列表函数
     */
    public function postlist()
    {
        $list = $this->modelpost->postlist();
        $this->assign('list', $list);
        return view("postlist", [
            'user' => $this->user
        ]);
    }

    /**
     * 帖子编辑函数
     */
    public function postEdit()
    {
        $data = [
            'create_time' =>time(),
            'content' => $_POST['content'],
        ];
        $id = $_POST["id"];
        $res = $this->modelpost->postEdit($data,$id);
        if ($res) {
           echo 1;
        }else{
           echo 0;
        }
    }

    /**
     * 帖子回复函数
     */
    public function postreply()
    {
        $pid = $_POST['id'];
        $user = session('user');
        $user_id = $user['id'];
        $user_name = $user['user_name'];
        $content = $_POST['content'];
        
        $data = [
            'title' => "{$user_name}回复说:",
            'pid' => $pid,
            'user_id' => $user_id,
            'content' => $content,
            'create_time' => date("Y-m-d H:i:s", time())
        ];
        $res = $this->modelpost->postreply($data);
        if ($res) {
            echo 1;  
        }else{
            echo 0;
        } 
       }
    
    public function quit()
    {
        $this->user = NULL;
        session("user", NULL);
        echo "登录";
    }
}